package com.PengGoBook.main.ui;

import android.view.View;
import android.widget.AbsListView;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-6-4
 * Time: 下午7:54
 * To change this template use File | Settings | File Templates.
 */
public class    AutoLoadListener implements AbsListView.OnScrollListener {

    public interface AutoLoadCallBack{
        void excute(String url);
    }

    private AutoLoadCallBack mCallBack;
    private int getLastVisiblePosition=0,lastVisiblePositionY=0;
    public AutoLoadListener(AutoLoadCallBack callBack){
          this.mCallBack = callBack;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //To change body of implemented methods use File | Settings | File Templates.
        if(scrollState== AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
                /*滑动到底部*/
             if(view.getLastVisiblePosition() == view.getCount()-1){
                   View v = view.getChildAt(view.getChildCount()-1);
                   int[] location = new int[2];
                 v.getLocationOnScreen(location);
                 int y = location[1];

                 if(view.getLastVisiblePosition()!= getLastVisiblePosition && lastVisiblePositionY!=y){
                     getLastVisiblePosition = view.getLastVisiblePosition();
                     lastVisiblePositionY = y;
                    return;
                 }else if(view.getLastVisiblePosition() == getLastVisiblePosition && lastVisiblePositionY == y){
                     mCallBack.excute("1");
                 }
             }
            getLastVisiblePosition = 0;
            lastVisiblePositionY = 0;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
